#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    vector<int> sortedSquares(vector<int> &nums)
    {
        vector<int> ret(nums.size());
        int l = 0, r = nums.size() - 1;
        int k = nums.size() - 1;
        while (l < r)
        {
            if (nums[l] * nums[l] > nums[r] * nums[r])
            {
                ret[k--] = nums[l] * nums[l];
                l++;
            }
            else
            {
                ret[k--] = nums[r] * nums[r];
                r--;
            }
        }
        ret[0] = (nums[l] * nums[l]);
        return ret;
    }
};